package com.hsxxherp.ation.oauth2.center.server.controller;

import com.hsxxherp.ation.oauth2.center.server.entity.LoginRequest;
import com.hsxxherp.ation.oauth2.center.server.feign.UcUserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 授权登录类
 */
@Slf4j
@Controller("/OAuth2")
public class Authorize {

    @Autowired
    UcUserFeign ucUserFeign;


    /**
     * 获取用户登录信息,登录成功页面重定向到redirect_uri，并携带token和state
     *
     * @param clientId
     * @param clientSecret
     * @param responseType
     * @param redirectUri
     * @param state
     * @param lang
     * @return
     */
    @GetMapping(value = "/OAuth2/authorize")
    public String authorize(@RequestParam(value = "client_id", required = false) String clientId, @RequestParam(value = "client_secret", required = false) String clientSecret, @RequestParam(value = "response_type", required = false) String responseType, @RequestParam(value = "redirect_uri", required = false) String redirectUri, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "lang", required = false) String lang, Model model) {
        model.addAttribute("clientId", clientId);
        model.addAttribute("clientSecret", clientSecret);
        model.addAttribute("responseType", responseType);
        model.addAttribute("redirectUri", redirectUri);
        model.addAttribute("state", state);
        model.addAttribute("lang", lang);
        log.info("\n {};\n {},\n {},\n {},\n {},\n {}", clientId, clientSecret, responseType, redirectUri, state, lang);
        return "authorize";
    }

    /**
     * 登录接口
     *
     * @param loginRequest
     * @return
     */
    @ResponseBody
    @PostMapping("/OAuth2/login")
    public boolean login(@RequestBody LoginRequest loginRequest) {
        return ucUserFeign.LoginByEmailRequest(loginRequest);
    }
}
